const app = getApp();
Page({
  data: {
    path:"../index/index",
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },
  onLoad(query) {
    var q = "";
    var obj = query;
    console.log(query)
    for (var k in obj) {
      q += k + "=" + obj[k] + "&"
    } 
    // we7 / pages / mine / minePSIntegralExchange?oder_id=300
    console.log(q)
    this.setData({
      path: query.p +"?"+ q
    })
    // 查看是否授权
    // wx.getSetting({
    //   success(res) {
    //     if (res.authSetting['scope.userInfo']){
    //       wx.getUserInfo({
    //         success(res){
    //           wx.redirectTo({
    //             url: query.p
    //           })
    //         }
    //       })
    //     }
    //   }
    // })
  },
  bindGetUserInfo(e){
    const that=this;
    wx.getSetting({
      success(res) {
        if (res.authSetting['scope.userInfo']) {
          wx.getUserInfo({
            success(a){
              console.log(a.userInfo)
              // console.log(a.userInfo.nickName)
              wx.setStorageSync('avatarUrl', a.userInfo.avatarUrl)
              wx.setStorageSync('nickName', a.userInfo.nickName)
              var str = JSON.parse(a.rawData);
              
              wx.login({
                success(res){
                  if (res.code){
                    app.util.request({
                      url: 'admin/WxGoodsApi/wxUser',
                      data:{
                        code: res.code,
                        nickName: str['nickName'],
                        avatarUrl: str['avatarUrl'],
                        city: str['city'],
                        country: str['country'],
                        gender: str['gender'],
                        language: str['language']
                      },
                      method: 'POST',
                      header:{
                        'content-type': 'application/x-www-form-urlencoded'
                      },
                      success: function(res){
                        console.log(res)
                        wx.setStorageSync('openid', res.data.data.openid)
                        wx.redirectTo({
                          url:"/"+that.data.path
                        })
                      }
                    });
                  } else {
                    console.log('登录失败！' + res.errMsg)
                  }
                }
              })
            }
          })
        }
      }
    })
  }
})